/*
 * Copyright (c) 2024 endless-sky
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FOUNDATION_ALIYUN_INCLUDE_CLOUD_MODEL_INIT_H_
#define FOUNDATION_ALIYUN_INCLUDE_CLOUD_MODEL_INIT_H_

#include <mutex>
#include <nlohmann/json.hpp>
#include "utils_noncopyable.h"

namespace El {
namespace Cloud {

/**
 * @brief 阿里云服务管理类（单例模式）
 * 统一管理阿里云AI服务的生命周期，包括图像识别、音频处理和诊断服务
 */
class AliyunService : public NoCopyable {
public:
    /**
     * @brief 获取单例实例
     * @return AliyunService& 单例实例引用
     */
    static AliyunService &GetInstance();

    /**
     * @brief 启动阿里云服务
     * 初始化所有云模型组件
     * @return bool true=成功，false=失败
     */
    bool Start();

    /**
     * @brief 停止阿里云服务
     * 释放所有云模型组件资源
     * @return bool true=成功，false=失败
     */
    bool Stop();

    /**
     * @brief 检查服务是否已启动
     * @return bool true=已启动，false=未启动
     */
    bool IsStarted() const;

    /**
     * @brief 启动实时音频对话（手动模式专用）
     * @return bool true=成功，false=失败
     */
    bool StartRealtimeAudio();

    /**
     * @brief 停止实时音频对话（手动模式专用）
     * @return bool true=成功，false=失败
     */
    bool StopRealtimeAudio();

    /**
     * @brief 获取实时音频状态
     * @return nlohmann::json 包含mode, state, stream_active等信息
     */
    nlohmann::json GetRealtimeAudioStatus() const;

private:
    AliyunService();
    ~AliyunService();

    // 实时音频自动模式初始化
    bool StartRealtimeAudioAutoMode();

private:
    mutable std::mutex mutex_;
    bool started_;

    // 实时音频管理
    bool autoModeEnabled_{false};  // 是否启用自动模式
};

} // namespace Cloud
} // namespace El

#endif // FOUNDATION_ALIYUN_INCLUDE_CLOUD_MODEL_INIT_H_
